package com.key.win.business.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.key.win.base.exception.BizException;
import com.key.win.base.page.MybatisPageServiceTemplate;
import com.key.win.base.web.PageRequest;
import com.key.win.base.web.PageResult;
import com.key.win.business.chat.dao.ImGroupNoticeDao;
import com.key.win.business.chat.model.ImGroup;
import com.key.win.business.chat.model.ImGroupNotice;
import com.key.win.business.chat.service.ImGroupNoticeService;
import com.key.win.business.chat.service.ImGroupService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class ImGroupNoticeServiceImpl extends ServiceImpl<ImGroupNoticeDao, ImGroupNotice> implements ImGroupNoticeService {

    @Autowired
    private ImGroupService imGroupService;

    /**
     * 删除公告后
     * 清空组里面的公告信息和公告Id
     *
     * @param id
     * @return
     */
    public boolean deleteById(String id) {
        ImGroup imGroup = new ImGroup();
        imGroup.setNoticeId(id);
        List<ImGroup> groups = imGroupService.findGroup(imGroup);
        if (!CollectionUtils.isEmpty(groups)) {
            ImGroup imGroup1 = groups.get(0);
            if (imGroupService.clearNotice(imGroup1.getId())) {
                return this.removeById(id);
            }
        }
        return Boolean.FALSE;
    }

    @Override
    public PageResult<ImGroupNotice> findImGroupNoticeByPaged(PageRequest<ImGroupNotice> t) {
        MybatisPageServiceTemplate<ImGroupNotice, ImGroupNotice> query = new MybatisPageServiceTemplate<ImGroupNotice, ImGroupNotice>(this.baseMapper) {
            @Override
            protected AbstractWrapper constructWrapper(ImGroupNotice imGroupNotice) {
                return buildImGroupNoticeLambdaQueryWrapper(imGroupNotice);
            }
        };
        return query.doPagingQuery(t);
    }

    @Override
    public List<ImGroupNotice> findImGroupNotice(ImGroupNotice imGroupNotice) {
        LambdaQueryWrapper<ImGroupNotice> lambdaQueryWrapper = buildImGroupNoticeLambdaQueryWrapper(imGroupNotice);
        return this.list(lambdaQueryWrapper);
    }

    private LambdaQueryWrapper<ImGroupNotice> buildImGroupNoticeLambdaQueryWrapper(ImGroupNotice imGroupNotice) {
        LambdaQueryWrapper<ImGroupNotice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (imGroupNotice != null) {
            if (StringUtils.isNotBlank(imGroupNotice.getNotice())) {
                lambdaQueryWrapper.like(ImGroupNotice::getNotice, imGroupNotice.getNotice());
            }
        }
        return lambdaQueryWrapper;
    }

    @Override
    public boolean saveOrUpdateImGroupNotice(ImGroupNotice imGroupNotice) {
        ImGroupNotice po = null;
        if (imGroupNotice == null) {
            log.error("imGroupNotice is null！");
            throw new BizException("公告信息不存在");
        }
        if (StringUtils.isNotBlank(imGroupNotice.getId())) {
            po = this.getById(imGroupNotice.getId());
            po.setNotice(imGroupNotice.getNotice());
        } else {
            po = imGroupNotice;
        }
        return this.saveOrUpdate(po);
    }

    @Override
    public ImGroupNotice getImGroupNoticeById(String id) {
        return this.getById(id);
    }
}
